package com.wangmeng.book.server.action;

import com.wangmeng.book.server.Command;
import com.wangmeng.book.server.CommandResult;
import com.wangmeng.book.utils.ConstValueUtils;
import com.wangmeng.book.utils.SpringContextManager;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * User: 王猛
 * Date: 13-9-29
 */
public class BookFirstDealAction implements IBaseAction {

    public CommandResult execute(Command cmd) {
        CommandResult cr = new CommandResult();
        cr.setCode(CommandResult.CODE_SUCCESS);
        if (cmd.getCmdCode() == Command.CMD_BOOK_RECEIVE) {
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            String updateSql = "update book_document set approve_status = ?,part_name = ? where approve_status = ? and oid = ? ";
            Map<String, Object> params = cmd.getData();
            List<Integer> suList = new ArrayList<Integer>();
            if (params != null) {
                List<Map> items = (List<Map>) params.get("data");
                if (items != null) {
                    for (Map item : items) {
                        Integer oid = (Integer) item.get("oid");
                        String code = (String) item.get("code");
                        int rowNum = jt.update(updateSql, new Object[]{ConstValueUtils.APPROVE_RECEIVE, code, ConstValueUtils.APPROVE_INIT, oid});
                        if (rowNum > 0) {
                            suList.add(oid);
                        }
                    }
                }
            }
            params.clear();
            params.put("data",suList);
            cr.setData(params);
        } else if (cmd.getCmdCode() == Command.CMD_BOOK_REJECT) {
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            String updateSql = "update book_document set approve_status = ? where approve_status = ? and oid = ? ";
            Map<String, Object> params = cmd.getData();
            List<Integer> suList = new ArrayList<Integer>();
            if (params != null) {
                List<Map> items = (List<Map>) params.get("data");
                if (items != null) {
                    for (Map item : items) {
                        Integer oid = (Integer) item.get("oid");
                        int rowNum = jt.update(updateSql, new Object[]{ConstValueUtils.APPROVE_REJECT, ConstValueUtils.APPROVE_INIT, oid});
                        if (rowNum > 0) {
                            suList.add(oid);
                        }
                    }
                }
            }
            params.clear();
            params.put("data",suList);
            cr.setData(params);
        }
        return cr;
    }
}
